{ "cells": [ { "cell_type": "markdown", "id": "44ca09e5", "metadata": {}, "source": [ "# Programming Techniques — Laboratory\n", "## Input, Type Conversion and Strings\n", "\n", "This laboratory follows the order used in class, but the exercises deliberately **combine several concepts**.\n", "\n", "### Topics\n", "- `input()`\n", "- type conversion with `int()`, `float()` and `str()`\n", "- `int()` with a **base** (binary, hexadecimal, ...)\n", "- common conversion errors\n", "- `len()` with strings\n", "- character indexes\n", "- slicing, including negative steps\n", "- escape sequences\n", "- functions vs. methods\n", "- string methods: `.lower()`, `.upper()`, `.replace()`\n", "- f-strings\n", "\n", "### Working style\n", "1. **Guided exercise** — follow the instructor hands-on.\n", "2. **Laboratory exercises** — work semi-independently; ask questions and discuss your choices.\n", "3. **Independent review** — more complete exercises for consolidation and homework.\n", "\n", "> In this notebook we use strings as sequences. We do **not** use lists or loops yet." ] }, { "cell_type": "markdown", "id": "3874d145", "metadata": {}, "source": [ "---\n", "# Part C — Independent Review\n", "\n", "These exercises combine several ideas from the lesson. Complete them independently where possible." ] }, { "cell_type": "markdown", "id": "ededa7a2", "metadata": {}, "source": [ "## 7. Invoice line generator\n", "\n", "### Theory recall\n", "\n", "You can combine input, conversion, string operations and f-strings in one small program.\n", "\n", "```python\n", "quantity = int(input(\"Quantity: \"))\n", "price = float(input(\"Price: \"))\n", "```\n", "\n", "and:\n", "\n", "```python\n", "f\"{price:.2f}\"\n", "```\n", "\n", "formats a floating-point number using two decimal places.\n", "\n", "### Task\n", "\n", "Ask for:\n", "- product name;\n", "- product code;\n", "- quantity;\n", "- unit price.\n", "\n", "Then:\n", "1. convert the product name to uppercase;\n", "2. calculate the line total;\n", "3. extract the first three characters of the product code;\n", "4. print a formatted summary including product, prefix, quantity, unit price, total, and the length of the original product name." ] }, { "cell_type": "code", "execution_count": null, "id": "1e60cf32", "metadata": {}, "outputs": [], "source": [ "# TODO: read the input data\n", "# TODO: transform/extract/calculate\n", "# TODO: print the invoice line" ] }, { "cell_type": "markdown", "id": "497e77f8", "metadata": {}, "source": [ "## 8. Shipment identifier decoder\n", "\n", "### Theory recall\n", "\n", "Structured strings often contain information at known positions.\n", "\n", "Example:\n", "\n", "```text\n", "LIS-2026-0487\n", "```\n", "\n", "### Task\n", "\n", "Ask for an identifier in that format and extract:\n", "- location: `LIS`;\n", "- year: `2026`;\n", "- serial number: `0487`.\n", "\n", "Then print:\n", "- the three extracted fields;\n", "- the serial number as an integer;\n", "- the complete identifier reversed;\n", "- the location in lowercase;\n", "- a sentence such as:\n", "\n", "```text\n", "Shipment 487 was registered in lis in 2026.\n", "```\n", "\n", "Hint: `int(\"0487\")` produces `487`." ] }, { "cell_type": "code", "execution_count": null, "id": "467574fa", "metadata": {}, "outputs": [], "source": [ "shipment = input(\"Shipment identifier (e.g. LIS-2026-0487): \")\n", "\n", "# TODO: extract the fields using slicing\n", "# TODO: convert the serial number\n", "# TODO: transform and print the requested information" ] }, { "cell_type": "markdown", "id": "b3ef323d", "metadata": {}, "source": [ "## 9. Compact performance report\n", "\n", "### Theory recall\n", "\n", "A useful program often combines numeric input, string transformation, repeated characters, escape sequences and formatted output.\n", "\n", "### Task\n", "\n", "Ask for:\n", "- employee name;\n", "- department;\n", "- completed tasks;\n", "- target number of tasks.\n", "\n", "Produce output similar to:\n", "\n", "```text\n", "PERFORMANCE REPORT\n", "Employee: ANA SILVA\n", "Department: finance\n", "\n", "Completed: ********\n", "Target: **********\n", "\n", "Ana Silva completed 8 tasks out of 10.\n", "```\n", "\n", "Requirements:\n", "- `.upper()` for the displayed name;\n", "- `.lower()` for the department;\n", "- repeated `*` for completed and target;\n", "- at least one escape sequence;\n", "- an f-string for the final sentence;\n", "- print the length of the original employee name.\n", "\n", "Do not use loops or conditionals." ] }, { "cell_type": "code", "execution_count": null, "id": "ff70fc47", "metadata": {}, "outputs": [], "source": [ "# TODO: read the input values\n", "# TODO: transform the strings and build the histogram bars\n", "# TODO: print the report" ] }, { "cell_type": "markdown", "id": "da4f7fba", "metadata": {}, "source": [ "## 10. Bonus challenge — hexadecimal reference number\n", "\n", "### Theory recall\n", "\n", "`int(text, base)` interprets a string according to the selected base:\n", "\n", "```python\n", "int(\"1010\", 2) # 10\n", "int(\"2A\", 16) # 42\n", "```\n", "\n", "### Task\n", "\n", "A legacy system exports order references as hexadecimal text.\n", "\n", "Ask for:\n", "- customer name;\n", "- hexadecimal order reference.\n", "\n", "Then:\n", "1. convert the hexadecimal reference to a Python integer;\n", "2. print the customer name in uppercase;\n", "3. print both the original hexadecimal text and the converted decimal value;\n", "4. print the hexadecimal text reversed;\n", "5. report its length.\n", "\n", "Try also an invalid hexadecimal value such as `2G7F` and explain the error." ] }, { "cell_type": "code", "execution_count": null, "id": "aaeb2bd5", "metadata": {}, "outputs": [], "source": [ "# TODO: implement the hexadecimal reference converter" ] }, { "cell_type": "markdown", "id": "17caeeb7", "metadata": {}, "source": [ "---\n", "# Final checklist\n", "\n", "After this laboratory, you should be able to explain and use:\n", "\n", "- why `input()` returns a string;\n", "- when and why to use `int()`, `float()` and `str()`;\n", "- how `int(text, base)` interprets binary or hexadecimal strings;\n", "- why incompatible conversions can raise `ValueError`;\n", "- `len()` on strings;\n", "- positive and negative indexes;\n", "- slicing with `start:stop:step`;\n", "- reverse slicing with a negative step;\n", "- common escape sequences such as `\\n` and `\\t`;\n", "- the difference between calling a function and calling a method;\n", "- `.lower()`, `.upper()` and `.replace()`;\n", "- f-strings for readable formatted output." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }